//
// sparse.cpp
//
// Code generation for function 'sparse'
//

// Include files
#include "sparse.h"
#include "anonymous_function.h"
#include "detrendIBI_priors_data.h"
#include "detrendIBI_priors_internal_types.h"
#include "eml_int_forloop_overflow_check.h"
#include "introsort.h"
#include "rt_nonfinite.h"
#include "sparse1.h"
#include "coder_array.h"
#include "mwmathutil.h"

// Variable Definitions
static emlrtRSInfo nc_emlrtRSI{
    119,             // lineNo
    "sparse/sparse", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pathName
};

static emlrtRSInfo oc_emlrtRSI{
    120,             // lineNo
    "sparse/sparse", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pathName
};

static emlrtRSInfo pc_emlrtRSI{
    126,             // lineNo
    "sparse/sparse", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pathName
};

static emlrtRSInfo qc_emlrtRSI{
    138,             // lineNo
    "sparse/sparse", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pathName
};

static emlrtRSInfo tc_emlrtRSI{
    147,             // lineNo
    "sparse/sparse", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pathName
};

static emlrtRSInfo vc_emlrtRSI{
    209,             // lineNo
    "sparse/sparse", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pathName
};

static emlrtRSInfo yc_emlrtRSI{
    1672,          // lineNo
    "locSortrows", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pathName
};

static emlrtRSInfo ad_emlrtRSI{
    1674,          // lineNo
    "locSortrows", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pathName
};

static emlrtRSInfo bd_emlrtRSI{
    1675,          // lineNo
    "locSortrows", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pathName
};

static emlrtRSInfo od_emlrtRSI{
    15,    // lineNo
    "max", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\datafun\\max.m" // pathName
};

static emlrtRSInfo
    pd_emlrtRSI{
        44,         // lineNo
        "minOrMax", // fcnName
        "D:\\Program "
        "Files\\MATLAB\\R2022a\\toolbox\\eml\\eml\\+coder\\+internal\\minOrMax."
        "m" // pathName
    };

static emlrtRSInfo
    qd_emlrtRSI{
        79,        // lineNo
        "maximum", // fcnName
        "D:\\Program "
        "Files\\MATLAB\\R2022a\\toolbox\\eml\\eml\\+coder\\+internal\\minOrMax."
        "m" // pathName
    };

static emlrtRSInfo rd_emlrtRSI{
    226,             // lineNo
    "unaryMinOrMax", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\eml\\+coder\\+"
    "internal\\unaryMinOrMax.m" // pathName
};

static emlrtRSInfo sd_emlrtRSI{
    313,                     // lineNo
    "unaryMinOrMaxDispatch", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\eml\\+coder\\+"
    "internal\\unaryMinOrMax.m" // pathName
};

static emlrtRSInfo td_emlrtRSI{
    354,          // lineNo
    "minOrMax1D", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\eml\\+coder\\+"
    "internal\\unaryMinOrMax.m" // pathName
};

static emlrtRTEInfo n_emlrtRTEI{
    111,             // lineNo
    35,              // colNo
    "sparse/sparse", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pName
};

static emlrtRTEInfo o_emlrtRTEI{
    116,             // lineNo
    35,              // colNo
    "sparse/sparse", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pName
};

static emlrtRTEInfo p_emlrtRTEI{
    148,             // lineNo
    43,              // colNo
    "sparse/sparse", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pName
};

static emlrtRTEInfo q_emlrtRTEI{
    153,             // lineNo
    43,              // colNo
    "sparse/sparse", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pName
};

static emlrtRTEInfo pc_emlrtRTEI{
    125,      // lineNo
    44,       // colNo
    "sparse", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\shared\\coder\\coder\\+coder\\+internal\\@"
    "sparse\\sparse.m" // pName
};

static emlrtRTEInfo qc_emlrtRTEI{
    13,       // lineNo
    1,        // colNo
    "sparse", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\sparfun\\sparse.m" // pName
};

// Function Definitions
namespace coder {
void c_sparse(const emlrtStack *sp,
              const ::coder::array<int32_T, 1U> &varargin_1,
              const ::coder::array<int32_T, 1U> &varargin_2,
              const ::coder::array<real_T, 1U> &varargin_3, real_T varargin_4,
              real_T varargin_5, sparse *y)
{
  anonymous_function b_this;
  array<int32_T, 1U> sortedIndices;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  emlrtStack e_st;
  emlrtStack f_st;
  emlrtStack g_st;
  emlrtStack h_st;
  emlrtStack i_st;
  emlrtStack st;
  int32_T i;
  int32_T maxr;
  int32_T nc;
  int32_T numalloc;
  int32_T y_tmp_tmp;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  d_st.prev = &c_st;
  d_st.tls = c_st.tls;
  e_st.prev = &d_st;
  e_st.tls = d_st.tls;
  f_st.prev = &e_st;
  f_st.tls = e_st.tls;
  g_st.prev = &f_st;
  g_st.tls = f_st.tls;
  h_st.prev = &g_st;
  h_st.tls = g_st.tls;
  i_st.prev = &h_st;
  i_st.tls = h_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b((emlrtCTX)sp);
  st.site = &mc_emlrtRSI;
  nc = varargin_2.size(0);
  if ((varargin_1.size(0) != varargin_2.size(0)) ||
      (varargin_3.size(0) != varargin_2.size(0))) {
    emlrtErrorWithMessageIdR2018a(&st, &n_emlrtRTEI, "MATLAB:samelen",
                                  "MATLAB:samelen", 0);
  }
  if ((varargin_3.size(0) != varargin_2.size(0)) &&
      (varargin_3.size(0) != varargin_1.size(0))) {
    emlrtErrorWithMessageIdR2018a(&st, &o_emlrtRTEI, "MATLAB:samelen",
                                  "MATLAB:samelen", 0);
  }
  b_st.site = &nc_emlrtRSI;
  sparse::assertValidIndexArg(&b_st, varargin_1, b_this.workspace.b);
  b_st.site = &oc_emlrtRSI;
  sparse::assertValidIndexArg(&b_st, varargin_2, b_this.workspace.a);
  sortedIndices.set_size(&pc_emlrtRTEI, &st, varargin_2.size(0));
  b_st.site = &pc_emlrtRSI;
  if (varargin_2.size(0) > 2147483646) {
    c_st.site = &y_emlrtRSI;
    check_forloop_overflow_error(&c_st);
  }
  for (int32_T k{0}; k < nc; k++) {
    sortedIndices[k] = k + 1;
  }
  b_st.site = &qc_emlrtRSI;
  c_st.site = &yc_emlrtRSI;
  internal::introsort(&c_st, sortedIndices, b_this.workspace.a.size(0),
                      &b_this);
  c_st.site = &ad_emlrtRSI;
  sparse::permuteVector(&c_st, sortedIndices, b_this.workspace.a);
  c_st.site = &bd_emlrtRSI;
  sparse::permuteVector(&c_st, sortedIndices, b_this.workspace.b);
  b_st.site = &rc_emlrtRSI;
  c_st.site = &nd_emlrtRSI;
  if ((varargin_4 != varargin_4) || muDoubleScalarIsInf(varargin_4)) {
    emlrtErrorWithMessageIdR2018a(
        &c_st, &j_emlrtRTEI, "Coder:MATLAB:NonIntegerInput",
        "Coder:MATLAB:NonIntegerInput", 4, 12, MIN_int32_T, 12, MAX_int32_T);
  }
  if (varargin_4 < 0.0) {
    emlrtErrorWithMessageIdR2018a(&b_st, &r_emlrtRTEI,
                                  "Coder:toolbox:SparseNegativeSize",
                                  "Coder:toolbox:SparseNegativeSize", 0);
  }
  if (!(varargin_4 < 2.147483647E+9)) {
    emlrtErrorWithMessageIdR2018a(
        &b_st, &s_emlrtRTEI, "Coder:toolbox:SparseMaxSize",
        "Coder:toolbox:SparseMaxSize", 2, 12, MAX_int32_T);
  }
  b_st.site = &sc_emlrtRSI;
  c_st.site = &nd_emlrtRSI;
  if ((varargin_5 != varargin_5) || muDoubleScalarIsInf(varargin_5)) {
    emlrtErrorWithMessageIdR2018a(
        &c_st, &j_emlrtRTEI, "Coder:MATLAB:NonIntegerInput",
        "Coder:MATLAB:NonIntegerInput", 4, 12, MIN_int32_T, 12, MAX_int32_T);
  }
  if (varargin_5 < 0.0) {
    emlrtErrorWithMessageIdR2018a(&b_st, &r_emlrtRTEI,
                                  "Coder:toolbox:SparseNegativeSize",
                                  "Coder:toolbox:SparseNegativeSize", 0);
  }
  if (!(varargin_5 < 2.147483647E+9)) {
    emlrtErrorWithMessageIdR2018a(
        &b_st, &s_emlrtRTEI, "Coder:toolbox:SparseMaxSize",
        "Coder:toolbox:SparseMaxSize", 2, 12, MAX_int32_T);
  }
  y->m = static_cast<int32_T>(varargin_4);
  y_tmp_tmp = static_cast<int32_T>(varargin_5);
  y->n = static_cast<int32_T>(varargin_5);
  if (b_this.workspace.b.size(0) != 0) {
    b_st.site = &tc_emlrtRSI;
    c_st.site = &od_emlrtRSI;
    d_st.site = &pd_emlrtRSI;
    e_st.site = &qd_emlrtRSI;
    f_st.site = &rd_emlrtRSI;
    numalloc = b_this.workspace.b.size(0);
    g_st.site = &sd_emlrtRSI;
    maxr = b_this.workspace.b[0];
    h_st.site = &td_emlrtRSI;
    if (b_this.workspace.b.size(0) > 2147483646) {
      i_st.site = &y_emlrtRSI;
      check_forloop_overflow_error(&i_st);
    }
    for (int32_T k{2}; k <= numalloc; k++) {
      i = b_this.workspace.b[k - 1];
      if (maxr < i) {
        maxr = i;
      }
    }
    if (maxr > static_cast<int32_T>(varargin_4)) {
      emlrtErrorWithMessageIdR2018a(
          &st, &p_emlrtRTEI, "Coder:builtins:IndexOutOfBounds",
          "Coder:builtins:IndexOutOfBounds", 6, 12, maxr, 12, 1, 12,
          static_cast<int32_T>(varargin_4));
    }
  }
  if ((b_this.workspace.a.size(0) != 0) &&
      (b_this.workspace.a[b_this.workspace.a.size(0) - 1] >
       static_cast<int32_T>(varargin_5))) {
    emlrtErrorWithMessageIdR2018a(
        &st, &q_emlrtRTEI, "Coder:builtins:IndexOutOfBounds",
        "Coder:builtins:IndexOutOfBounds", 6, 12,
        b_this.workspace.a[b_this.workspace.a.size(0) - 1], 12, 1, 12,
        static_cast<int32_T>(varargin_5));
  }
  i = varargin_2.size(0);
  numalloc = muIntScalarMax_sint32(i, 1);
  y->d.set_size(&qc_emlrtRTEI, &st, numalloc);
  for (i = 0; i < numalloc; i++) {
    y->d[i] = 0.0;
  }
  y->maxnz = numalloc;
  if (static_cast<int32_T>(varargin_5) + 1 < 0) {
    emlrtNonNegativeCheckR2012b(
        static_cast<real_T>(static_cast<int32_T>(varargin_5) + 1), &d_emlrtDCI,
        &st);
  }
  y->colidx.set_size(&rc_emlrtRTEI, &st, static_cast<int32_T>(varargin_5) + 1);
  y->colidx[0] = 1;
  y->rowidx.set_size(&qc_emlrtRTEI, &st, numalloc);
  for (i = 0; i < numalloc; i++) {
    y->rowidx[i] = 0;
  }
  numalloc = 0;
  b_st.site = &uc_emlrtRSI;
  if (static_cast<int32_T>(varargin_5) > 2147483646) {
    c_st.site = &y_emlrtRSI;
    check_forloop_overflow_error(&c_st);
  }
  for (maxr = 0; maxr < y_tmp_tmp; maxr++) {
    while ((numalloc + 1 <= nc) && (b_this.workspace.a[numalloc] == maxr + 1)) {
      y->rowidx[numalloc] = b_this.workspace.b[numalloc];
      numalloc++;
    }
    y->colidx[maxr + 1] = numalloc + 1;
  }
  b_st.site = &vc_emlrtRSI;
  for (int32_T k{0}; k < nc; k++) {
    y->d[k] = varargin_3[sortedIndices[k] - 1];
  }
  b_st.site = &wc_emlrtRSI;
  y->fillIn();
  emlrtHeapReferenceStackLeaveFcnR2012b((emlrtCTX)sp);
}

} // namespace coder

// End of code generation (sparse.cpp)
